//
//  M4NetworkAlertView.swift
//  M4
//
//  Created by YuChuang on 2024/8/22.
//

import UIKit

class M4NetworkAlertView: UIView {
    
    @IBOutlet weak var M4NetRefreshButton: UIButton!
    
    @IBOutlet weak var M4NetCellularButton: UIButton!
    
    @IBOutlet weak var M4NetWIFIButton: UIButton!
    
    static var shared: M4NetworkAlertView = {
           return UINib(nibName: "M4NetworkAlertView", bundle: nil)
               .instantiate(withOwner: nil)
               .last as! M4NetworkAlertView
       }()

       func showOnce() {
           if self.superview == nil {
               M4NetworkAlertView.shared.M4NetworkAlertShow()
           }
       }
    override func awakeFromNib() {
        self.frame = CGRect(x: 0, y: 0, width: M4ScreenWidth, height: M4ScreenHeight)
        self.tag = 3333
        M4NetRefreshButton.layer.cornerRadius = 2
        M4NetRefreshButton.layer.borderWidth = 0.5
        M4NetRefreshButton.layer.borderColor = COLOR_00479D.cgColor
        M4NetCellularButton.layer.cornerRadius = 2
        M4NetCellularButton.layer.borderWidth = 0.5
        M4NetCellularButton.layer.borderColor = COLOR_00479D.cgColor
        M4NetWIFIButton.layer.cornerRadius = 2
        M4NetWIFIButton.layer.borderWidth = 0.5
        M4NetWIFIButton.layer.borderColor = COLOR_00479D.cgColor
        self.layer.zPosition = 1
    }
    
    func M4NetworkAlertShow() {
        if M4NetworkAlertView.M4AlertInKeywindow() != nil {return}
        
        
        if let topVC = UIApplication.shared.windows.first?.rootViewController {
            topVC.view.addSubview(self)
//            UIApplication.shared.keyWindow?.addSubview(self)
            self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
            UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut) {
                self.alpha = 1
                self.transform = CGAffineTransform.identity
            }
        }
    }
    
    class func M4AlertInKeywindow() -> UIView? {
        for dtView in UIApplication.shared.keyWindow?.subviews ?? [] {
            if dtView.tag == 3333 {
                return dtView
            }
        }
        return nil
    }
    
    @IBAction func M4NetRefreshAction(_ sender: UIButton) {
        self.M4AlertViewHidden()
        DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
            M4BaseViewController.M4CurrentVC()?.M4DataRefresh()
        }
        sender.M4EventUnavailable = false
    }
    
    @IBAction func M4NetCellularSetting(_ sender: UIButton) {
        guard let url = URL(string: "App-Prefs:Cellular") else { return }
        if  UIApplication.shared.canOpenURL(url) {
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:],completionHandler: {(success) in})
            } else {
                UIApplication.shared.openURL(url)
            }
        }
        self.M4AlertViewHidden()
        sender.M4EventUnavailable = false
    }
    
    @IBAction func M4NetWiFISetting(_ sender: UIButton) {
        guard let url = URL(string: "App-Prefs:WIFI") else { return }
        if  UIApplication.shared.canOpenURL(url) {
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:],completionHandler: {(success) in})
            } else {
                UIApplication.shared.openURL(url)
            }
        }
        self.M4AlertViewHidden()
        sender.M4EventUnavailable = false
    }
    

}
